package com.group56.view.userend.Log_register_module;

import com.group56.CoachApp;
import com.group56.CustomerApp;
import com.group56.GymApp;
import com.group56.TestApp;
import com.group56.model.dao.CustomerDAO;
import com.group56.model.dao.Impl.CoachDAOImpl;
import com.group56.model.dao.Impl.CustomerDAOImpl;
import com.group56.model.entity.Coach;
import com.group56.model.entity.Customer;
import com.group56.utils.FXMLSrcMenu;
import com.group56.utils.FXMLViewHandler;
import javafx.fxml.FXML;


import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;

public class LoginController  {
    @FXML
    private Button Go_register;
    @FXML
    private Button log_in;
    @FXML
    private TextField username;
    @FXML
    private PasswordField password;
    @FXML
    private ToggleGroup identity;
    public void submit() throws IOException {
        System.out.println(checkInfo());
        if (checkInfo()&&identity.getSelectedToggle().getUserData().equals("customer")){
            System.out.println("用户成功登陆");
            GymApp.customerId = new CustomerDAOImpl().selectByName(username.getText()).getCustomerID();
            Stage primaryStage=(Stage)log_in.getScene().getWindow();
            primaryStage.hide();
//            FXMLViewHandler.putViewInStage(primaryStage, FXMLSrcMenu.UserEnd.APP_FRAMEWORK.getSrc(), "方案二");
//            primaryStage.show();
        }
        if (checkInfo()&&identity.getSelectedToggle().getUserData().equals("coach")){
            System.out.println("教练登陆成功");
            CoachApp.coachId = new CoachDAOImpl().selectByName(username.getText()).getCoachID();

            Stage primaryStage=(Stage)log_in.getScene().getWindow();
            primaryStage.hide();
        }
    }
    public boolean checkInfo(){
        boolean pass=false;
        String user_name=username.getText();
        System.out.println(user_name);
        String pass_word=password.getText();
//        System.out.println(identity.getSelectedToggle().getUserData());
        if (identity.getSelectedToggle().getUserData().equals("customer")) {

            CustomerDAOImpl customerDAO = new CustomerDAOImpl();

            Customer customer=customerDAO.selectByName(user_name);
            System.out.println(customer);
            if (customer!=null){
                if (customer.getPassword().equals(pass_word)){
                    pass=true;
                    TestApp.customerId=customer.getCustomerID();
                }
            }
        }
        if (identity.getSelectedToggle().getUserData().equals("coach")) {
            CoachDAOImpl coachDAO= new CoachDAOImpl();
            Coach coach=coachDAO.selectByName(user_name);
            if (coach!=null){
                if (coach.getPassword().equals(pass_word)){
                    pass=true;
                }
            }
        }
        return pass;
    }

    public void GoToRegister() throws IOException {
        Stage primaryStage=(Stage)log_in.getScene().getWindow();
        primaryStage.hide();
        FXMLViewHandler.putViewInStage(primaryStage, FXMLSrcMenu.UserEnd.REGISTER.getSrc(), "login/register");
        primaryStage.show();
    }

}
